package com.biubiu.newapplication.activity

import android.content.Context
import android.os.Bundle
import android.text.ClipboardManager
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import androidx.core.view.isVisible
import com.biubiu.newapplication.R
import com.biubiu.newapplication.activity.module.UniversalAdapterActivity
import com.biubiu.newapplication.base.BaseActivity
import com.biubiu.newapplication.databinding.ActivityMainBinding


class MainActivity: BaseActivity<ActivityMainBinding>() {

    private lateinit var adapter: ArrayAdapter<String>
    override val layoutId: Int
        get() = R.layout.activity_main

    override fun initView(savedInstanceState: Bundle?) {
        //状态栏颜色
        setStatusBarColor("#FF0000")
        val data = arrayOf(
            getString(R.string.module_adapter),
        )
        adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data)
        val listView = findViewById<ListView>(R.id.list)
        listView.adapter = adapter
        listView.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ ->
            onItemClick(position)
        }
        mDataBinding.ivClose.setOnClickListener {
            hideInstructions()
        }
        mDataBinding.ivCopy.setOnClickListener {
            copyString(mDataBinding.tvContent.text.toString())
        }
    }

    private fun onItemClick(position: Int) {
        //item点击事件
        when(adapter.getItem(position)){
            getString(R.string.module_adapter) -> {
                startActivity(UniversalAdapterActivity::class.java)
            }
        }
    }

    override fun onBackPressed() {
        if(mDataBinding.llInstructions.isVisible){
            hideInstructions()
            return
        }
        super.onBackPressed()
    }

    private fun showInstructions(content: String) {
        mDataBinding.llInstructions.isVisible = true
        mDataBinding.tvContent.text = content
    }

    private fun hideInstructions() {
        mDataBinding.llInstructions.isVisible = false
        mDataBinding.tvContent.text = ""
    }

    private fun copyString(content: String) {
        val cm = this.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        cm.text = content
        Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show()
    }
}